package com.testdocs.client.structure.top;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.testdocs.client.common.DropDownListWidget;
import com.testdocs.client.common.DropDownListWidget.Order;
import com.testdocs.client.project.ProjectInfo;
import com.testdocs.client.structure.top.i18n.TopPanelConstants;

public class TopPanelImpl extends Composite implements TopPanel {
	@UiTemplate("TopPanel.ui.xml")
	interface TopPanelUiBinder extends UiBinder<Widget, TopPanelImpl> {
	}

	private static TopPanelUiBinder uiBinder = GWT
			.create(TopPanelUiBinder.class);

	@UiField
	Label userName;
	@UiField
	Label projectLabel;
	@UiField
	SimplePanel projectNamePanel;
	@UiField
	SimplePanel statusBarPanel;
	@UiField
	Anchor signOutAnchor;
	
	DropDownListWidget projectDropDown = new DropDownListWidget();
	
	private Presenter presenter;
	
	private TopPanelConstants topPanelConstants = (TopPanelConstants) GWT.create(TopPanelConstants.class);
	
	private Command createProjectCommand = new Command(){
		@Override
		public void execute() {
			presenter.onCreateNewProject();
		}
	};

	private class SelectProjectCommans implements Command {
		private ProjectInfo project;
		
		public SelectProjectCommans(ProjectInfo project) {
			this.project = project;
		}
		
		@Override
		public void execute() {
			presenter.onProjectSelect(project);
		}
	}

	public TopPanelImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		projectLabel.setText(topPanelConstants.projectLabel());
		projectDropDown.addItem("new", topPanelConstants.createNewProject(), createProjectCommand, Order.BOTTOM);
		projectNamePanel.add(projectDropDown);
		setSignedOutView();
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@UiHandler("signOutAnchor")
	public void onSignOutClicked(ClickEvent event) {
		if (presenter != null) {
			presenter.onSignOut();
		}
	}

	public void setSignOutUrl(String href) {
		signOutAnchor.setText(topPanelConstants.signOut());
		signOutAnchor.setHref(href);
	}

	@Override
	public void setProjectInfo(ProjectInfo project) {
		projectDropDown.setItemSelected(project.getProjectId());
	}

	@Override
	public void setNoProjectSelected() {
		projectDropDown.setItemSelected("new");
	}

	@Override
	public void setUser(String user) {
		userName.setText(user);
	}
	
	@Override
	public void setProjectsList(ProjectInfo[] projects) {
		for (ProjectInfo project: projects) {
			projectDropDown.addItem(project.getProjectId(), project.getName(), new SelectProjectCommans(project), Order.TOP);					
		}
	}

	@Override
	public void addProjectToList(ProjectInfo project) {
		projectDropDown.addItem(project.getProjectId(), project.getName(), new SelectProjectCommans(project), Order.TOP);					
	}

	@Override
	public void removeProjectFromList(ProjectInfo project) {
		projectDropDown.removeItem(project.getProjectId());					
	}

	@Override
	public void setSignedOutView() {
		projectLabel.setVisible(false);
		projectDropDown.setVisible(false);
	}
	
	public void setSignedInView() {
		projectLabel.setVisible(true);
		projectDropDown.setVisible(true);		
	}
	
	@Override
	public void setStatusBar(Widget widget) {
		statusBarPanel.setWidget(widget);
	}
}
